package com.itheima.web; /**
 * @author Xiao
 * @date 2021/8/31
 */

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/selectByIdServletJson")
public class SelectByIdServletJson extends HttpServlet {
    private BrandService brandService = new BrandService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jsonId = request.getReader().readLine();
        Brand brand1 = JSON.parseObject(jsonId, Brand.class);
        Brand brand = brandService.selectById(brand1.getId());
        String jsonStr = JSON.toJSONString(brand);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(jsonStr);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
